package com.module.config;

import com.module.manager.CustomCacheManager;
import com.module.utils.CacheKeyGenerator;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.CacheKeyPrefix;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import java.net.URISyntaxException;
import java.time.Duration;

/**
 * 缓存配置
 * @author chenluqiang
 * @date 2019/2/28
 */
@EnableCaching
@Configuration
public class CacheConfig {


	/*@Autowired
    private RedisConnectionFactory factory;*/

	/**
     * redis 自定义manager
     * @author chenluqiang
     * @date 2019/2/28
     * @param [redisTemplate]
     * @return org.springframework.data.redis.cache.RedisCacheManager
     */
   /* @Bean("redisCacheManager")
    public RedisCacheManager redisCacheManager(@Qualifier("redisTemplate")RedisTemplate redisTemplate) {
        //设置数据存入 redis 的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(null);
        redisTemplate.setValueSerializer(null);
        return new RedisCacheManager(redisTemplate);
    }*/
    
    /**
     * redis 自定义manager  springboot 2.x配置方式
     * @author chenluqiang
     * @date 2019/2/28
     * @param [redisTemplate]
     * @return org.springframework.data.redis.cache.RedisCacheManager
     */
    @Bean("redisCacheManager")
    public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
    	System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++redisCacheManager");
    	 RedisCacheConfiguration redisCacheConfiguration=RedisCacheConfiguration
    			 .defaultCacheConfig()
//    			 .entryTtl(Duration.ofHours(1)) // 设置缓存有效期一小时
                //缓存分割符 默认为 ":"
    			 //自定义key值格式
                 /*.computePrefixWith(new CacheKeyPrefix() {
					@Override
					public String compute(String cacheName) {
						return cacheName+"::";
					}
				})*/
                 //不使用前缀。
//                 .disableKeyPrefix()
                 ;
         return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                 .cacheDefaults(redisCacheConfiguration).build();
    }

   /**
    *  jCacheCache 自定义manager
    * @author chenluqiang
    * @date 2019/2/28
    * @param []
    * @return org.springframework.cache.jcache.JCacheCacheManager
    */
    @Bean("jCacheCacheManager")
    public JCacheCacheManager jCacheCacheManager() throws URISyntaxException {
    	 System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++jCacheCacheManager");
        CachingProvider provider = Caching.getCachingProvider();
        JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
        CacheManager eh107CacheManager = provider.getCacheManager(getClass().getResource("/ehcache/ehcache.xml").toURI(), getClass().getClassLoader());
        jCacheCacheManager.setCacheManager(eh107CacheManager);
        return jCacheCacheManager;
    }

    /**
     * 自定义缓存管理器，用于切换缓存。
     * @author chenluqiang
     * @date 2019/2/28
     * @param redisCacheManager, jCacheCacheManager]
     * @return CacheManager
     */
    @Bean("cacheManager")
    @Primary
    public CustomCacheManager initMixCacheManager(RedisCacheManager redisCacheManager,JCacheCacheManager jCacheCacheManager) {
        CustomCacheManager cacheManager = new CustomCacheManager();
        System.out.println("++++++++++++++++++++++++cacheManager++++++++++++++++++++++++++++++");
        cacheManager.setRedisCacheManager(redisCacheManager);
        cacheManager.setJCacheCacheManager(jCacheCacheManager);
        return cacheManager;
    }
    
    @Bean("keyGenerator")
    public KeyGenerator keyGenerator() {
        return new CacheKeyGenerator();
    }

    /**
     * 实例化 HashOperations 对象,可以使用 Hash 类型操作
     */

    /**
     * 实例化 ValueOperations 对象,可以使用 String 操作
     */

    /**
     * 实例化 ListOperations 对象,可以使用 List 操作
     */

    /**
     * 实例化 SetOperations 对象,可以使用 Set 操作
     */

    /**
     * 实例化 ZSetOperations 对象,可以使用 ZSet 操作
     */
}
